﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample610
{
    class Program
    {
        static async Task Main(string[] args)
        {
            CreateISEA(true);
            var h = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) config.AddVulkanJsonFile("Vulkan.json");
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) config.AddVulkanJsonFile("Vulkan-Linux.json");
                    else throw new NotSupportedException("Not supported Platform");
                })
                .ConfigureSgrAHost((sgra) =>
                {
                    sgra.AddVulkanEnvironmental();
                    sgra.UseStartup<Startup>();
                }).ConfigureLogging((logger) => logger.AddConsole()); ;
            await h.RunConsoleAsync();
        }

        static void CreateISEA(bool isNeed)
        {
            var texPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Textures");
            var imageJson = JsonDocument.Parse(File.ReadAllText(Path.Combine(texPath, "vulkan.json")));
            var vulkanJson = imageJson.RootElement.GetProperty("Vulkan").GetRawText();
            var samplerJson = imageJson.RootElement.GetProperty("Sampler").GetRawText();
            ISEADocument document = new ISEADocument();
            ConfigNode vulkanConfig = document.CreateConfigNode("Config", vulkanJson, ConfigEngine.Vulkan);
            ConfigNode sampler = document.CreateConfigNode("Sampler", samplerJson, ConfigEngine.Vulkan);
            IOP.ISEA.ImageNode image = document.CreateImageNode("VulkanImage", Path.Combine(texPath, "vulkan.bntexa"));
            var vData = new float[15]
            {
                0,10,0, 0.5f,0,
                -9,-5,0, 0,1,
                9,-5,0, 1,1
            };
            MeshNode mesh = document.CreateEmptyMeshNode($"VulkanMesh", 3)
                .AddMeshData(vData, 20, 0, MeshDataPurpose.COMPLEX, false);
            TextureNode tex = document.CreateTextureNode($"Tex", TextureUsage.CombinedImageSampler, image,
                vulkanConfig, sampler, 1);
            document.CreateRenderObjectNode($"Vulkan", mesh, tex);
            document.Save(AppContext.BaseDirectory, "Test610");
        }
    }
}
